package com.jhgk.jhcee.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jhgk.jhcee.common.enums.BasisResultEnum;
import com.jhgk.jhcee.common.resultful.BasisResultful;
import com.jhgk.jhcee.data.auth.entity.SysModuleInfo;
import com.jhgk.jhcee.data.auth.mapper.SysModuleInfoMapper;
import com.jhgk.jhcee.system.domain.model.SysModuleInfoComparator;
import com.jhgk.jhcee.system.service.SysModuleInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;

/**
 * @Description:
 * @Author: lemon_xzp
 * @Date: 2021/9/25 16:42
 */
@Service(value = "SysModuleInfoServiceImpl")
public class SysModuleInfoServiceImpl extends ServiceImpl<SysModuleInfoMapper,SysModuleInfo> implements SysModuleInfoService {

    @Autowired
    SysModuleInfoMapper sysModuleInfoMapper;

    @Override
    public BasisResultful<Object> getModuleList() {
        List<SysModuleInfo> sysModuleInfos = sysModuleInfoMapper.selectList(null);
        if (!sysModuleInfos.isEmpty()) {
            sysModuleInfos.sort(new SysModuleInfoComparator());
        }
        sysModuleInfos.sort(Comparator.comparing(SysModuleInfo::getSort));
        return BasisResultful.success(sysModuleInfos);
    }

    @Override
    public BasisResultful<Object> getModuleResourcesByName(String moduleName) {
        if (StringUtils.isBlank(moduleName)) {
            return BasisResultful.fail(BasisResultEnum.PARAM_IS_NULL);
        }
        QueryWrapper<SysModuleInfo> wrapper = new QueryWrapper<>();
        wrapper.like("module_name", moduleName);
        List<SysModuleInfo> sysModuleInfos = sysModuleInfoMapper.selectList(wrapper);
        if (!sysModuleInfos.isEmpty()) {
            sysModuleInfos.sort(new SysModuleInfoComparator());
        }
        return BasisResultful.success(sysModuleInfos);
    }

    @Override
    public BasisResultful<Object> getModuleResourcesByModuleId(Long moduleId) {
        QueryWrapper<SysModuleInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("module_id", moduleId);
        List<SysModuleInfo> sysModuleInfos = sysModuleInfoMapper.selectList(wrapper);
        if (!sysModuleInfos.isEmpty()) {
            sysModuleInfos.sort(new SysModuleInfoComparator());
        }
        return BasisResultful.success(sysModuleInfos);
    }

    @Override
    public BasisResultful<Object> getTreeModuleResources() {

        return null;
    }
}
